﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;
    using WMS6.Web.UserControls;

    public class WaveList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnClose;
        protected HtmlInputButton btnConfirm;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F7192;
        protected GridView gvWave;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlInputHidden hdnUserValue;
        protected HtmlInputHidden hdnWaveIDs;
        protected HtmlHead Head1;
        protected HtmlInputHidden hiddenFlag;
        protected HtmlInputHidden hiddenReportFlag;
        protected HtmlInputButton htmlbtnReport;
        protected Label lblBatchOrderList;
        protected Label lblMsg;
        protected LinkButton lnkbtnAllocate;
        protected LinkButton lnkbtnCloseOrder;
        protected LinkButton lnkbtnPackaging;
        protected LinkButton lnkbtnPreallocate;
        protected LinkButton lnkbtnPrintDeliveryOrder;
        protected LinkButton lnkbtnPrintExpressOrder;
        protected LinkButton lnkbtnPrintOrderInvoice;
        protected LinkButton lnkbtnPrintPicking;
        protected LinkButton lnkbtnPrintShippingOrder;
        protected LinkButton lnkbtnPrintWaveShipment;
        protected LinkButton lnkbtnReConfirm;
        protected LinkButton lnkbtnReleasePickTasks;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnAllocate;
        protected HtmlTableCell tdlnkbtnCloseOrder;
        protected HtmlTableCell tdlnkbtnPackaging;
        protected HtmlTableCell tdlnkbtnPreallocate;
        protected HtmlTableCell tdlnkbtnPrintDeliveryOrder;
        protected HtmlTableCell tdlnkbtnPrintExpressOrder;
        protected HtmlTableCell tdlnkbtnPrintOrderInvoice;
        protected HtmlTableCell tdlnkbtnPrintPicking;
        protected HtmlTableCell tdlnkbtnPrintShippingOrder;
        protected HtmlTableCell tdlnkbtnPrintWaveShipment;
        protected HtmlTableCell tdlnkbtnReConfirm;
        protected HtmlTableCell tdlnkbtnReleasePickTasks;
        protected HtmlInputText txtUserID;
        protected UpdatePanel up1;

        private void BindDataWaveList(WaveQueryEntity waveQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(waveQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OutboundServiceClientProxy.GetInstance().GetWaveByQueryList(waveQuery);
            base.BindData2GridView(this.gvWave, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvWave, "WAVE_ID");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_004");
                }
                else
                {
                    string result = OutboundService2ClientProxy.GetInstance().WaveReleasePickTasks(selectedID, this.hdnUserValue.Value);
                    if (!string.IsNullOrEmpty(result))
                    {
                        this.lblMsg.Text = result + base.GetGlobalMsgResource("F7300_012");
                    }
                    this.LoadData();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);$.unblockUI();</script>", false);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> Status = base.GetSelectedIDsInGridView(this.gvWave, "STATUS");
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvWave, "WAVE_ID");
                if ((Status != null) && (Status.Count > 0))
                {
                    for (int i = 0; Status.Count > i; i++)
                    {
                        if (Status[i].ToUpper() != "NEW")
                        {
                            this.lblMsg.Text = "状态不等于New无法删除!";
                            return;
                        }
                    }
                }
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_004");
                }
                else
                {
                    this.lblMsg.Text = OutboundServiceClientProxy.GetInstance().DeleteWave(selectedID) + base.GetGlobalMsgResource("F7300_005");
                    this.LoadData();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvWave, "WAVE_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_004");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_022");
            }
            else
            {
                base.Response.Redirect(string.Format("Wave.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            WaveQueryEntity waveQuery = this.GetWaveQueryEntity();
            waveQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = waveQuery.CurrentPage;
            waveQuery.CurrentPage = "0";
            this.gvWave.Columns[1].Visible = false;
            this.gvWave.AllowSorting = false;
            this.BindDataWaveList(waveQuery);
            this.hdnActionID.Value = "";
            this.gvWave.ShowFooter = false;
            waveQuery.IsGetAll = false;
            waveQuery.CurrentPage = currentPageIndex;
            this.SetWaveQueryEntity(waveQuery);
            base.Export2Excel("BatchOrder.xls", this.gvWave);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            WaveQueryEntity waveQuery = this.GetWaveQueryEntity();
            waveQuery.CurrentPage = "0";
            this.BindDataWaveList(waveQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            WaveQueryEntity waveQuery = this.GetWaveQueryEntity();
            waveQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataWaveList(waveQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            WaveQueryEntity waveQuery = this.GetWaveQueryEntity();
            waveQuery.WaveID = "";
            waveQuery.WaveType = "";
            waveQuery.ExternalWaveID = "";
            waveQuery.Status = "";
            waveQuery.Descr = "";
            waveQuery.CreatedBy = "";
            this.BindDataWaveList(waveQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            WaveQueryEntity waveQuery = this.GetWaveQueryEntity();
            if (int.Parse(waveQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                waveQuery.CurrentPage = (int.Parse(waveQuery.CurrentPage) + 1).ToString();
                this.BindDataWaveList(waveQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            WaveQueryEntity waveQuery = this.GetWaveQueryEntity();
            if (int.Parse(waveQuery.CurrentPage) > 0)
            {
                waveQuery.CurrentPage = (int.Parse(waveQuery.CurrentPage) - 1).ToString();
                this.BindDataWaveList(waveQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            this.LoadData();
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvWave, "WAVE_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_004");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_022");
            }
            else
            {
                base.Response.Redirect(string.Format("Wave.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        private WaveQueryEntity GetWaveQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as WaveQueryEntity);
            }
            return (base.PageQueryEntitySession as WaveQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                WaveQueryEntity waveQuery = this.GetWaveQueryEntity();
                waveQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataWaveList(waveQuery);
            }
        }

        protected void gvWave_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7192", this.gvWave);
        }

        protected void gvWave_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvWave_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            this.hdnRtnValue.Value = base.UrlReplace(this.gvWave.DataKeys[e.NewSelectedIndex]["WAVE_ID"].ToString());
            if (!string.IsNullOrEmpty(this.hdnRtnValue.Value))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + this.hdnRtnValue.Value + "';  window.close();</script>", false);
            }
        }

        protected void gvWave_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                WaveQueryEntity waveQuery = this.GetWaveQueryEntity();
                waveQuery.SortField = e.SortExpression;
                if (waveQuery.SortDirection == "ASC")
                {
                    waveQuery.SortDirection = "DESC";
                }
                else if (waveQuery.SortDirection == "DESC")
                {
                    waveQuery.SortDirection = "ASC";
                }
                this.BindDataWaveList(waveQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnClosed_Click(object sender, EventArgs e)
        {
            string result = string.Empty;
            try
            {
                List<string> waveID = base.GetSelectedIDsInGridView(this.gvWave, "WAVE_ID");
                List<string> status = base.GetSelectedIDsInGridView(this.gvWave, "STATUS");
                for (int i = 0; status.Count > i; i++)
                {
                    if ((status[i].ToUpper() != "PARTSHIPPED") && (status[i].ToUpper() != "SHIPPED"))
                    {
                        this.lblMsg.Text = "状态不等于部分发货或者发货,无法关闭！";
                        return;
                    }
                }
                result = OutboundServiceClientProxy.GetInstance().BatchClosed(waveID);
                this.LoadData();
                this.lblMsg.Text = "单据 " + result + " 关闭成功!";
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnPrintWaveShipment_Click(object sender, EventArgs e)
        {
            this.hdnWaveIDs.Value = string.Empty;
            string waveIDs = string.Empty;
            try
            {
                List<string> waveID = base.GetSelectedIDsInGridView(this.gvWave, "WAVE_ID");
                if ((waveID == null) || (waveID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_015");
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2); </script>", false);
                    return;
                }
                for (int i = 0; waveID.Count > i; i++)
                {
                    waveIDs = waveIDs + "'" + waveID[i] + "',";
                }
                this.hdnWaveIDs.Value = waveIDs.Substring(0, waveIDs.Length - 1);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2); </script>", false);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2);popPrintWaveShipment(); </script>", false);
        }

        protected void lnkbtnReleasePickTasks_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> waveID = base.GetSelectedIDsInGridView(this.gvWave, "WAVE_ID");
                string result = OutboundService2ClientProxy.GetInstance().WaveReleasePickTasks(waveID, "");
                if (!string.IsNullOrEmpty(result))
                {
                    this.lblMsg.Text = result + base.GetGlobalMsgResource("F7300_012");
                    this.LoadData();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);$.unblockUI();</script>", false);
            }
        }

        private void LoadData()
        {
            WaveQueryEntity waveQuery = new WaveQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    waveQuery.WaveID = this.PreviousPage.WaveID;
                    waveQuery.ExternalWaveID = this.PreviousPage.ExternalWaveID;
                    waveQuery.Status = this.PreviousPage.Status;
                    waveQuery.Descr = this.PreviousPage.Description;
                    waveQuery.CreatedBy = this.PreviousPage.CreatedBy;
                }
                else
                {
                    waveQuery.WaveID = "";
                    waveQuery.WaveType = "";
                    waveQuery.ExternalWaveID = "";
                    waveQuery.Status = "";
                    waveQuery.Descr = "";
                    waveQuery.CreatedBy = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            waveQuery.IsGetAll = false;
            waveQuery.CurrentPage = "0";
            waveQuery.PageSize = this.gvWave.PageSize.ToString();
            waveQuery.SortField = "W.ROWID";
            waveQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvWave.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvWave.Columns[1].Visible = false;
                this.gvWave.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
                this.btnAction.Visible = false;
            }
            this.SetWaveQueryEntity(waveQuery);
            this.BindDataWaveList(waveQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetWaveQueryEntity(WaveQueryEntity waveQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = waveQuery;
            }
            else
            {
                base.PageQueryEntitySession = waveQuery;
            }
        }

        public WaveQuery PreviousPage
        {
            get
            {
                return (WaveQuery) base.PreviousPage;
            }
        }
    }
}

